function test(resolve, reject) //new promise(function(resolve,reject)) 函数参数需要两个参数resolve,reject。所以test才有两个参数
{
    var timeOut = Math.random() * 2;
    console.log('set timeout to: ' + timeOut + ' seconds.');
    setTimeout(function ()
    {
        if (timeOut < 1) //小于1成功
        {
            console.log('call resolve()...');
            resolve('200 OK');
        }
        else 
        {
            console.log('call reject()...');
            reject('timeout in ' + timeOut + ' seconds.');
        }
    }, timeOut * 1000);
}
var p1 = new Promise(test);
var p2 = p1.then(function (result) 
{
    console.log('成功：' + result);
}).catch(function (reason) 
{
    console.log('失败：' + reason);
});;
